import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_base/dk_core.dart';

class InputStatefulWidget extends StatefulWidget {
  //占位符
  final String placeholder;
  //最多输入文字
  final int maxLength;
  //文本控制器
  final TextEditingController controller;
  //是否显示底部最多显示字数的文本
  final bool showWaringText;
  //焦点
  final FocusNode focusNode;
  //占位符风格
  final TextStyle placeholderStyle;
  //文字风格
  final TextStyle textStyle;
  //最多少字文字风格
  final TextStyle waringTextStyle;
  //文字背景颜色
  final Color textBgColor;
  //文字背景颜色
  final Color bgColor;
  //是否可编辑
  final bool readOnly;
  //内容区高度
  final double contentHeight;
  //按下return（完成键）键收起键盘
  final bool returnKeyDownKeyBoard;

  InputStatefulWidget({
    this.placeholder = "",
    this.maxLength = 100,
    @required this.controller,
    this.showWaringText = false,
    this.focusNode,
    this.textStyle,
    this.waringTextStyle,
    this.placeholderStyle,
    this.textBgColor,
    this.bgColor,
    this.readOnly = false,
    this.contentHeight = 65,
    this.returnKeyDownKeyBoard = true
  });
  @override
  _InputStatefulWidgetState createState() => _InputStatefulWidgetState();
}

class _InputStatefulWidgetState extends State<InputStatefulWidget> {
  final double countHeight = 20;

  int getMaxLength(){
    //设置了return键放下键盘和最大长度之后，应该设置最大长度再加1
    if(this.widget.returnKeyDownKeyBoard == true && this.widget.maxLength != null) {
      int maxLength = this.widget.maxLength + 1;
      return maxLength;
    } else {
      return this.widget.maxLength;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: this.widget.bgColor ?? Colors.white,
      padding: EdgeInsets.only(bottom: 8),
      child: Row(
        children: <Widget>[
          SizedBox(
            width: 16,
            height: 0,
          ),
          Expanded(
            child: Column(
              children: <Widget>[
                Container(
                  height: this.widget.showWaringText ? (this.widget.contentHeight + this.countHeight) : this.widget.contentHeight,
                  color: this.widget.textBgColor ?? Color(0xFFF7F8F9),
                  child: TextField(
                    autocorrect: false,
                    readOnly: this.widget.readOnly,
                    enableInteractiveSelection: false,
                    focusNode: this.widget.focusNode,
                    controller: this.widget.controller,
                    onChanged: (value) {
                      if (value.contains("\n") && this.widget.returnKeyDownKeyBoard == true) {
                        this.widget.controller.text = this
                            .widget
                            .controller
                            .text
                            .replaceAll("\n", "");
                        this.widget.focusNode.unfocus();
                      } else if(value.length > this.widget.maxLength && this.widget.returnKeyDownKeyBoard == true) {
                        this.widget.controller.text = this
                            .widget
                            .controller
                            .text
                            .replaceRange(this.widget.maxLength, this.widget.maxLength + 1, "");
                        this.widget.focusNode.unfocus();
                      }
                      setState(() {});
                    },
                    maxLength: this.getMaxLength(),
                    maxLines: null,
                    style: this.widget.textStyle ??
                        TextStyle(fontSize: 13, color: Color(0xFF666666)),
                    enabled: true,
                    autofocus: false,
                    decoration: InputDecoration(
                        contentPadding: const EdgeInsets.all(9),
                        border: InputBorder.none,
                        hintText: this.widget.placeholder,
                        hintStyle: this.widget.placeholderStyle ??
                            TextStyle(fontSize: 13, color: Color(0xFF999999)),
                        counterText: ""),
                  ),
                ),
                this.widget.showWaringText == false || this.widget.readOnly
                    ? SizedBox(
                        width: 0,
                        height: 0,
                      )
                    : Container(
                        height: 20,
                        color: Color(0xFFF7F8F9),
                        padding: EdgeInsets.only(right: 9, bottom: 4),
                        alignment: Alignment.centerRight,
                        child: CountWidget(
                          waringTextStyle: this.widget.waringTextStyle,
                          maxLength: this.widget.maxLength,
                          count: this.widget.controller.text.length,
                        ),
                      ),
              ],
            ),
          ),
          SizedBox(
            width: 16,
            height: 0,
          ),
        ],
      ),
    );
  }
}

class CountWidget extends StatelessWidget {
  final int maxLength;
  final int count;
  //最多少字文字风格
  final TextStyle waringTextStyle;
  CountWidget({this.maxLength, this.count, this.waringTextStyle});
  @override
  Widget build(BuildContext context) {
    return this.count == 0
        ? Text(
            "最多输入${this.maxLength}个字",
            style: this.waringTextStyle ??
                TextStyle(color: Color(0xFF999999), fontSize: 11),
          )
        : Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Text("${this.count}/",
                  style: TextStyle(color: Colors.black, fontSize: 11)),
              Text(
                "${this.maxLength}",
                style: TextStyle(color: Color(0xFFDBDBDB), fontSize: 11),
              ),
            ],
          );
  }
}
